<?php
/**
 * planning/popin-transfert.phtml
 */
?>
<?php

use RHX\Helper\RHDateTime;
use RHX\Model\Object\XRules;
?>
<?php
$transfert	 = $this->transfert;
$employe	 = $this->employe;
?>
<div class="popin-container">
    <div class="popin-content">
	<div class="popin-title content-head">
	    <h4>Edition du Transfert</h4>
	</div>
	<div class="popin-left">
	    <!-- INFO EMPLOYE -->
	    <div class="employe-name">
		<span class="value"><?php echo $employe->prenom . ' ' . $employe->nom1; ?></span>
	    </div>
	    <div class="popin-clear"></div>
	    <div class="employe-site">
		<?php $depart		 = $transfert->getSiteDepart(); ?>
		<span class="libelle">Site de Provenance : </span><br><span class="value"><?php echo $depart->libelle; ?></span>
	    </div>
	</div>
	<div class="popin-right">
	    <!-- HEURES -->
	    <?php
	    $entree		 = new RHDateTime();
	    $entree->setTime($transfert->entree);
	    ?>
	    <div class="heure-title">Heures d'entrée</div>
	    <select id="entree_heure">
		<?php for ($i = 0; $i < 24; $i++): ?>
		    <?php
		    $value		 = ($i < 10) ? '0' . $i : $i;
		    $selected	 = ((int) $entree->getH() == $i) ? ' selected="selected"' : '';
		    ?>
		<option value="<?php echo $value ?>"<?php echo $selected; ?>><?php echo $value; ?></option>
		<?php endfor; ?>
	    </select>
	    :
	    <select id="entree_minute">
		<?php for ($i = 0; $i < 60; $i+= 5): ?>
		    <?php
		    $value		 = ($i < 10) ? '0' . $i : $i;
		    $selected	 = ((int) $entree->getM() == $i) ? ' selected="selected"' : '';
		    ?>
		<option value="<?php echo $value ?>"<?php echo $selected; ?>><?php echo $value; ?></option>
		<?php endfor; ?>
	    </select>
	    <?php
	    $sortie = new RHDateTime();
	    $sortie->setTime($transfert->sortie);
	    ?>
	    <div class="heure-title">Heures de sortie</div>
	    <select id="sortie_heure">
		<?php for ($i = 0; $i < 24; $i++): ?>
		    <?php
		    $value		 = ($i < 10) ? '0' . $i : $i;
		    $selected	 = ((int) $sortie->getH() == $i) ? ' selected="selected"' : '';
		    ?>
		<option value="<?php echo $value ?>"<?php echo $selected; ?>><?php echo $value; ?></option>
		<?php endfor; ?>
	    </select>
	    :
	    <select id="sortie_minute">
		<?php for ($i = 0; $i < 60; $i+= 5): ?>
		    <?php
		    $value		 = ($i < 10) ? '0' . $i : $i;
		    $selected	 = ((int) $sortie->getM() == $i) ? ' selected="selected"' : '';
		    ?>
		<option value="<?php echo $value ?>"<?php echo $selected; ?>><?php echo $value; ?></option>
		<?php endfor; ?>
	    </select>
	    <hr>
	    <label class="popin-label">
		<span>Pause</span>
		<input type="checkbox" id="pause" <?php echo ((int) $transfert->pause == 1) ? 'checked="checked"' : ''; ?>>
	    </label>
	    <label class="popin-label">
		<span>Repas</span>
		<input type="checkbox" id="repas" <?php echo ((int) $transfert->repas == 1) ? 'checked="checked"' : ''; ?>>
	    </label>
	    <!-- PAUSE & REPAS -->
	</div>
	<!--div style="width: 99%; clear: both;">
	    Commentaire : <a href="#" id="commentaire" data-type="textarea"><?php echo $transfert->commentaire ?></a>
	</div-->
	<div class="popin-clear"></div>
	<hr>
	<div class="popin-action">
	    <button class="btn btn-success btn-small validate-change">Valider</button>
	    <button class="btn btn-inverse btn-small cancel-change">Annuler</button>
	    <button class="btn btn-danger btn-small delete-transfert" style="float: right;">Supprimer</button>
	</div>
    </div>
</div>

<form id="transfertEdit" action="#" method="post">
    <input type="hidden" name="id" value="<?php echo $transfert->id; ?>">
    <input type="hidden" name="depart" value="<?php echo $transfert->depart; ?>" id="data-depart">
    <input type="hidden" name="arrivee" value="<?php echo $transfert->arrivee; ?>" id="data-arrivee">
    <input type="hidden" name="matricule" value="<?php echo $transfert->matricule; ?>" id="data-matricule">
    <input type="hidden" name="jour" value="<?php echo $transfert->journee; ?>" id="data-jour">
    <input type="hidden" name="entree" value="<?php echo $transfert->entree; ?>" id="data-entree">
    <input type="hidden" name="sortie" value="<?php echo $transfert->sortie; ?>" id="data-sortie">
    <input type="hidden" name="pause" value="<?php echo $transfert->pause; ?>" id="data-pause">
    <input type="hidden" name="repas" value="<?php echo $transfert->repas; ?>" id="data-repas">
    <input type="hidden" name="repas" value="<?php echo $transfert->commentaire; ?>" id="data-commentaire">
</form>

<script type="text/javascript">
    $(function() {
	// validation
	$('.validate-change').click(function(e) {
	    e.preventDefault();
	    entree = $('#entree_heure option:selected').val() + ':' + $("#entree_minute option:selected").val();
	    sortie = $('#sortie_heure option:selected').val() + ':' + $("#sortie_minute option:selected").val();
	    pause = ($("#pause").prop('checked')) ? 1 : 0;
	    repas = ($('#repas').prop('checked')) ? 1 : 0;
	    date = $('#data-jour').val();
	    matricule = $('#data-matricule').val();
	    allow = $.ajax({
		type: 'post',
		url : '<?php echo $this->url('RHX/default', array('controller' => 'transfert', 'action' => 'allowTransfert')) ?>',
		data: '&id=<?php echo $transfert->id; ?>&matricule=' + matricule + '&date=' + date + '&entree=' + entree + '&sortie=' + sortie
	    });
	    allow.done(function(result){
		if(!result.allowed){
		    showError("Attention! Le transfert de l'employé ne peut aboutir<br>Il existe déjà un planning aux horaires indiquées<br>" + result.message);
		    return false;
		}
		ajax = $.ajax({
		    type: 'post',
		    url : '<?php echo $this->url('RHX/default', array('controller'	 => 'transfert', 'action'	 => 'editFromPlanning')) ?>',
		    data: '&id=<?php echo $transfert->id; ?>&entree=' + entree + '&sortie=' + sortie + '&pause=' + pause + "&repas=" + repas + '&commentaire=' + encodeURI($('#data-commentaire').val())
		});
		ajax.done(function(result){
		    if(result.done){
			// changement des valeurs
			selector = '#t-<?php echo $transfert->id; ?>';
			$(selector + '-entree a').text(entree);
			$(selector + '-sortie a').text(sortie);
			$(selector + '-pause a').text((pause === 1) ? 'X' : '');
			$(selector + '-repas a').text((repas === 1) ? '1' : '');
			$("#" + ID).click();
		    }
		    $('.cancel-change').click();
		});
	    });
	});
	// Annulation
	$('.cancel-change').click(function(e) {
	    e.preventDefault();
	    // Ferméture
	    $(".rhPopover").remove();
	});
	// Suppression
	$('.delete-transfert').click(function(e) {
	    e.preventDefault();
	    if (!confirm("Voulez-vous effacer le Transfert ?")) {
		return false;
	    }
	    id = <?php echo $transfert->id; ?>;
	    ajax = $.ajax({
		dataType: 'json',
		type: 'post',
		url: '<?php echo $this->url('RHX/default', array('controller'	 => 'transfert', 'action'	 => 'cancel')); ?>',
		data: '&id=' + id
	    });
	    ajax.done(function(data) {
		// Rafraichissement de la page
		$('.rhx-siteTitle').click();
	    });
	    $('.cancel-change').click();
	});
	// Commentaire
	$('#commentaire').editable({
	    title : "Edition du commentaire",
	    validate: function(value){
		$('#data-commentaire').val(value);
	    }
	});
	$('#entree_heure, #entree_minute, #sortie_heure, #sortie_minute').change(function(e){
	    e.preventDefault();
	    verifyHour();
	});
    });
    function verifyHour(){
	// Récupération des heures
	entree = $('#entree_heure option:selected').val() + ':' + $("#entree_minute option:selected").val();
	sortie = $('#sortie_heure option:selected').val() + ':' + $("#sortie_minute option:selected").val();
	// Traitement Ajax
	ajax = $.ajax({
	    type: 'post',
	    async: false,
	    url : '<?php echo $this->url('RHX/default', array('controller'	 => 'planning', 'action'	 => 'verifyHour')) ?>',
	    data: '&entree=' + entree + '&sortie=' + sortie
	});
	ajax.success(function(data){
	    if(data.pause){
		$("#pause").prop('checked', true);
	    }

	    if(data.repas){
		$("#repas").prop('checked', true);
	    }
	});
	ajax.error(function(){});
    }
</script>

<style type="text/css">
    .popin-container{
	position: absolute;
	width: 300px;
	background: #f6f6f6;
	border-radius: 0px 5px 0 5px;
	box-shadow: 0px 5px 5px 3px #895d5d;
    }
    .popin-left{
	float: left;
	width: 40%;
	/*background: #f8e8e8;*/
    }
    .popin-right{
	float: right;
	width: 50%;
	margin-left: 1%;
	/*background: #efefef;*/
    }
    .popin-clear{
	float:none;
	clear: both;
	margin: 5px auto;
    }
    .popin-content{
	border-radius: 0px 5px 0 5px;
    }
    .popin-content select{
	width: 40px;
    }
    .popin-title{
	margin: 0px auto 10px;
    }
    .popin-label{
	display: inline-block;
    }
    .libelle{
	text-decoration: underline;
    }
    .value{
	font-style: italic;
	font-weight: bold;
    }
    .employe-site{
	font-size: 1.1em;
    }
    .employe-name{
	font-size: 1.3em;
    }
</style>